import pygame
import sys
import requests
import time
pygame.init()
screen = pygame.display.set_mode((700,500))
pygame.display.set_caption("天气预报")
win_icon = pygame.image.load('weathericon/100.png')
pygame.display.set_icon(win_icon)
loc_url = "https://myip.ipip.net/json"
loc_json = requests.get(loc_url).json()
while loc_json['ret'] != 'ok':
    loc_json = requests.get(loc_url).json()
loc_data = loc_json['data']
location = loc_data['location'][2]
adm = loc_data['location'][1]
qw_key = "*************"
id_url = "https://geoapi.qweather.com/v2/city/lookup"
id_params = {
    'key': qw_key,
    'location': location,
    'adm': adm,
}
id_json = requests.get(id_url, params = id_params).json()
while id_json['code'] != '200':
    id_json = requests.get(id_url, params = id_params).json()
city_id = id_json['location'][0]['id']
fontname = 'AlibabaFont.otf'
font_a = pygame.font.Font(fontname, 20)
font_b = pygame.font.Font(fontname, 40)
font_c = pygame.font.Font(fontname, 28)
flush_icon = pygame.image.load('res/flush.png')
flush_icon = pygame.transform.scale(flush_icon, (100, 100))
flush_rect = pygame.Rect(550, 350, 100, 100)
city_info = ""
info_surf = font_a.render(city_info, True, (0, 0, 0))
weather_now = {}
def get_now():
    global weather_now, city_info, info_surf
    url = 'https://devapi.qweather.com/v7/weather/now'
    params = {
        'key': qw_key,
        'location': city_id,
    }
    now_json = requests.get(url, params = params).json()
    while now_json['code'] != '200':
        now_json = requests.get(url, params = params).json()
    weather_now = now_json['now']
    update_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    city_info = f"城市: {adm+location}  更新时间: {update_time}  数据来源: 和风天气"
    info_surf = font_a.render(city_info, True, (0, 0, 0))
get_now()
weather_pred = []
def get_pred():
    global weather_pred
    url = 'https://devapi.qweather.com/v7/weather/3d'
    params = {
        'key': qw_key,
        'location': city_id
    }
    pred_json = requests.get(url, params = params).json()
    while pred_json['code'] != '200':
        pred_json = requests.get(url, params = params).json()
    weather_pred = pred_json['daily']
get_pred()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if flush_rect.collidepoint(event.pos):
                get_now()
                get_pred()
    screen.fill((200, 200, 200))
    screen.blit(info_surf, (10, 20))
    icon = pygame.image.load('weathericon/{}.png'.format(weather_now['icon']))
    screen.blit(icon, (50, 100))
    temp = font_b.render(weather_now['temp']+'°', True, (0, 0, 0))
    screen.blit(temp, (180, 115))
    text = font_c.render(weather_now['text'], True, (0, 0, 0))
    screen.blit(text, (180, 165))
    
    wind_icon = pygame.image.load('res/wind.png')
    wind_text1 = font_c.render(weather_now['windScale'] + '级', True, (0, 0, 0))
    wind_text2 = font_a.render(weather_now['windDir'], True, (0, 0, 0))
    screen.blit(wind_icon, (300, 100))
    screen.blit(wind_text1, (370, 100))
    screen.blit(wind_text2, (370, 140))
    
    pcpn_icon = pygame.image.load('res/pcpn.png')
    pcpn_text1 = font_c.render(weather_now['precip'] + 'mm', True, (0, 0, 0))
    pcpn_text2 = font_a.render('小时降雨量', True, (0, 0, 0))
    screen.blit(pcpn_icon, (450, 100))
    screen.blit(pcpn_text1, (530, 100))
    screen.blit(pcpn_text2, (530, 140))
    
    hum_icon = pygame.image.load('res/hum.png')
    hum_text1 = font_c.render(weather_now['humidity'] + '%', True, (0, 0, 0))
    hum_text2 = font_a.render('湿度', True, (0, 0, 0))
    screen.blit(hum_icon, (300, 180))
    screen.blit(hum_text1, (370, 180))
    screen.blit(hum_text2, (370, 220))
    
    vis_icon = pygame.image.load('res/vis.png')
    vis_text1 = font_c.render(weather_now['vis'] + 'km', True, (0, 0, 0))
    vis_text2 = font_a.render('能见度', True, (0, 0, 0))
    screen.blit(vis_icon, (450, 180))
    screen.blit(vis_text1, (530, 180))
    screen.blit(vis_text2, (530, 220))
    
    predd1_icon1 = pygame.image.load('weathericon/{}.png'.format(weather_pred[0]['iconDay']))
    predd1_icon2 = pygame.image.load('weathericon/{}.png'.format(weather_pred[0]['iconNight']))
    predd1_icon1 = pygame.transform.scale(predd1_icon1, (30, 30))
    predd1_icon2 = pygame.transform.scale(predd1_icon2, (30, 30))
    predd1_text0 = font_a.render(weather_pred[0]['fxDate'], True, (0, 0, 0))
    predd1_text1 = font_a.render(weather_pred[0]['textDay'], True, (0, 0, 0))
    predd1_text2 = font_a.render(weather_pred[0]['textNight'], True, (0, 0, 0))
    predd1_text3 = font_a.render("高: {}°".format(weather_pred[0]['tempMax']), True, (0, 0, 0))
    predd1_text4 = font_a.render("低: {}°".format(weather_pred[0]['tempMin']), True, (0, 0, 0))
    screen.blit(predd1_text0, (50, 300))
    screen.blit(predd1_icon1, (170, 300))
    screen.blit(predd1_text1, (200, 300))
    screen.blit(predd1_icon2, (280, 300))
    screen.blit(predd1_text2, (310, 300))
    screen.blit(predd1_text3, (380, 300))
    screen.blit(predd1_text4, (450, 300))
    
    predd2_icon1 = pygame.image.load('weathericon/{}.png'.format(weather_pred[1]['iconDay']))
    predd2_icon2 = pygame.image.load('weathericon/{}.png'.format(weather_pred[1]['iconNight']))
    predd2_icon1 = pygame.transform.scale(predd2_icon1, (30, 30))
    predd2_icon2 = pygame.transform.scale(predd2_icon2, (30, 30))
    predd2_text0 = font_a.render(weather_pred[1]['fxDate'], True, (0, 0, 0))
    predd2_text1 = font_a.render(weather_pred[1]['textDay'], True, (0, 0, 0))
    predd2_text2 = font_a.render(weather_pred[1]['textNight'], True, (0, 0, 0))
    predd2_text3 = font_a.render("高: {}°".format(weather_pred[1]['tempMax']), True, (0, 0, 0))
    predd2_text4 = font_a.render("低: {}°".format(weather_pred[1]['tempMin']), True, (0, 0, 0))
    screen.blit(predd2_text0, (50, 370))
    screen.blit(predd2_icon1, (170, 370))
    screen.blit(predd2_text1, (200, 370))
    screen.blit(predd2_icon2, (280, 370))
    screen.blit(predd2_text2, (310, 370))
    screen.blit(predd2_text3, (380, 370))
    screen.blit(predd2_text4, (450, 370))
    
    predd3_icon1 = pygame.image.load('weathericon/{}.png'.format(weather_pred[2]['iconDay']))
    predd3_icon2 = pygame.image.load('weathericon/{}.png'.format(weather_pred[2]['iconNight']))
    predd3_icon1 = pygame.transform.scale(predd3_icon1, (30, 30))
    predd3_icon2 = pygame.transform.scale(predd3_icon2, (30, 30))
    predd3_text0 = font_a.render(weather_pred[2]['fxDate'], True, (0, 0, 0))
    predd3_text1 = font_a.render(weather_pred[2]['textDay'], True, (0, 0, 0))
    predd3_text2 = font_a.render(weather_pred[2]['textNight'], True, (0, 0, 0))
    predd3_text3 = font_a.render("高: {}°".format(weather_pred[2]['tempMax']), True, (0, 0, 0))
    predd3_text4 = font_a.render("低: {}°".format(weather_pred[2]['tempMin']), True, (0, 0, 0))
    screen.blit(predd3_text0, (50, 440))
    screen.blit(predd3_icon1, (170, 440))
    screen.blit(predd3_text1, (200, 440))
    screen.blit(predd3_icon2, (280, 440))
    screen.blit(predd3_text2, (310, 440))
    screen.blit(predd3_text3, (380, 440))
    screen.blit(predd3_text4, (450, 440))
    
    screen.blit(flush_icon, flush_rect)
    pygame.display.update()